package com.zeroone.star.mongo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zeroone.star.mongo.dto.MemberReadHistoryDTO;
import com.zeroone.star.mongo.entity.MemberReadHistory;
import com.zeroone.star.mongo.mapper.MemberReadHistoryRepository;
import com.zeroone.star.mongo.service.MemberReadHistoryService;
import com.zeroone.star.mongo.vo.MemberReadHistoryVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author lyx
 * @since 2024/11/17 15:27
 */
@Service
public class MemberReadHistoryServiceImpl implements MemberReadHistoryService {

    @Resource
    MemberReadHistoryRepository repository;


    @Override
    public int save(MemberReadHistoryDTO dto) {
        MemberReadHistory history = new MemberReadHistory();
        history.setCreateTime(new Date());
        BeanUtil.copyProperties(dto,history,true);
        repository.save(history);
        return 1;
    }

    @Override
    public int remove(List<String> ids) {
        List<MemberReadHistory> delList = new ArrayList<>();
        for (String id : ids) {
            MemberReadHistory history = new MemberReadHistory();
            history.setId(id);
            delList.add(history);
        }
        repository.deleteAll(delList);
        return 1;
    }

    @Override
    public List<MemberReadHistoryVO> list(Long memberId) {
        List<MemberReadHistory> result = repository.findByMemberIdOrderByCreateTimeDesc(memberId);
        ArrayList<MemberReadHistoryVO> vos = new ArrayList<>();
        for (MemberReadHistory history : result) {
            MemberReadHistoryVO vo = new MemberReadHistoryVO();
            BeanUtil.copyProperties(history, vo, true);
            vos.add(vo);
        }
        return vos;
    }
}
